package com.lankystar.logistucs.spring.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lankystar.logistucs.spring.dao.IBrokerDao;
import com.lankystar.logistucs.spring.dao.IOneOrderAndOneStatusDao;
import com.lankystar.logistucs.spring.dao.IOneOrderDao;
import com.lankystar.logistucs.spring.dao.IOrderStatusDao;
import com.lankystar.logistucs.spring.dao.ITwoOrderAndTwoStatusDao;
import com.lankystar.logistucs.spring.dao.ITwoOrderDao;
import com.lankystar.logistucs.spring.dao.IUserDao;
import com.lankystar.logistucs.spring.entity.Cargo;
import com.lankystar.logistucs.spring.entity.Company;
import com.lankystar.logistucs.spring.entity.OneOrder;
import com.lankystar.logistucs.spring.entity.OneOrderAndOneStatus;
import com.lankystar.logistucs.spring.entity.OneStatus;
import com.lankystar.logistucs.spring.entity.Truck;
import com.lankystar.logistucs.spring.entity.TwoOrder;
import com.lankystar.logistucs.spring.entity.TwoOrderAndTwoStatus;
import com.lankystar.logistucs.spring.entity.TwoStatus;
import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.service.MobileService;

@Service
public class MobileServiceImpl implements MobileService{

	@Autowired
	private IBrokerDao brokerDao;
	@Autowired
	private ITwoOrderDao twoOrderDao;
	@Autowired
	private IOneOrderDao oneOrderDao;
	@Autowired
	private IOrderStatusDao orderStatusDao;
	@Autowired
	private ITwoOrderAndTwoStatusDao tatDao;
	@Autowired
	private IUserDao userDao;
	@Autowired
	private IOneOrderAndOneStatusDao oaoDao;
	@Override
	public Object[] queryOneOrderAndOneStatus(int userId) {
		List<Object[]> oblist = oneOrderDao.selectOneOrderAndOneStatusByUserId(userId);
		int i =0;
		Object[] obj = new Object[oblist.size()];
			for (Object[] ob : oblist) {
				Map<String, Object> data = new HashMap<>();
				OneOrder oneOrder = (OneOrder) ob[3];
				OneOrderAndOneStatus oao = (OneOrderAndOneStatus) ob[4];
				OneStatus oneStatus = (OneStatus) ob[5];
				data.put("oneOrderId", oneOrder.getOneOrderId());
				data.put("oneStatusKey", oneStatus.getOneStatusKey());
				data.put("oneStatusValue", oneStatus.getOneStatusValue());
				data.put("createTime", oao.getCreateTime());
				obj[i] = data;
				i++;
			}
		return obj;
	}
	@Override
	public Object[] updateTwoOrder(int twoOrderId, int twoStatusKey,int userId) {
		User user = userDao.selectUserById(userId);
		Company cp = user.getCompany();
		TwoOrder twoOrder = null;
		TwoStatus twoStatus =null;
		TwoOrderAndTwoStatus tat =null;
		if(twoStatusKey==0){
			 twoOrder = twoOrderDao.selectTwoOrderById(twoOrderId);
			 twoStatus = orderStatusDao.selectTwoStatusBykey(1);
			 tat = new TwoOrderAndTwoStatus();
			tat.setTwoOrder(twoOrder);
			tat.setTwoStatus(twoStatus);
			tat.setCreateTime(new Date().getTime());
			tatDao.add(tat);
			
		}else{
			 twoOrder = twoOrderDao.selectTwoOrderById(twoOrderId);
			 twoStatus = orderStatusDao.selectTwoStatusBykey(twoStatusKey);
			 tat = new TwoOrderAndTwoStatus();
			tat.setTwoOrder(twoOrder);
			tat.setTwoStatus(twoStatus);
			tat.setCreateTime(new Date().getTime());
			tatDao.add(tat);
			/**
			 * 当上海报关结束，生成一级订单状态
			 */
			if(twoStatusKey==30){
				OneOrderAndOneStatus oao = new OneOrderAndOneStatus();
				OneOrder oneOrder = oneOrderDao.selectOneOrderByTwoOrderId(twoOrderId);
				OneStatus oneStatus = orderStatusDao.selectOneStatusBykey(twoStatusKey);
				oneOrder.setType(1);
				twoOrder.setType(1);
				oneOrderDao.update(oneOrder);
				twoOrderDao.update(twoOrder);
				oao.setOneOrder(oneOrder);
				oao.setOneStatus(oneStatus);
				oao.setCreateTime(new Date().getTime());
				oaoDao.add(oao);
			}
		}	
		List<Object[]> oblist = brokerDao.selectRecelveOrder(cp.getCompanyId(),String.valueOf(tat.getCreateTime()));
		Object[] objs = new Object[oblist.size()];
		int i = 0;
		for (Object[] obs : oblist) {
			Map<String, Object> map = new HashMap<>();
			TwoOrderAndTwoStatus tats = (TwoOrderAndTwoStatus) obs[1];
			TwoStatus twoStatuss = tats.getTwoStatus();
			TwoOrder twoOrders = (TwoOrder) obs[0];
			Truck truck = twoOrders.getTruck();//车辆
			//User user = twoOrder.getUser();//司机
			Cargo cargo = (Cargo) obs[3];
			map.put("twoOrderId", twoOrders.getTwoOrderId());
			map.put("totalOrderNu", cargo.getTotalOrderNu());
			map.put("pointOrderNu", cargo.getPointOrderNu());
			map.put("cardNu", truck.getCardNu());//白卡号
			map.put("uploadPdf", cargo.getUploadPdf());
			map.put("twoStatusKey", twoStatuss.getTwoStatusKey());
			map.put("twoStatusValue", twoStatuss.getTwoStatusValue());
			objs[i] = map;
			i++;
		}
		return objs;
	}
	@Override
	public Object[] queryCargoAndTwoOrder(int brokerId) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Object[] queryRecelveOrder(int brokerId, String createTime) {
		// TODO Auto-generated method stub
		return null;
	}

}
